import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'
import TabLayout from '@/layout/tab-layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    noCache: true                if set true, the page will no be cached(default is false)
    affix: true                  if set true, the tag will affix in the tags-view
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/auth-redirect',
    component: () => import('@/views/login/auth-redirect'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/error-page/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error-page/401'),
    hidden: true
  }
  // {
  //   path: '/',
  //   component: Layout,
  //   redirect: '/dashboard',
  //   children: [
  //     {
  //       path: 'dashboard',
  //       component: () => import('@/views/dashboard/index'),
  //       name: 'Dashboard',
  //       meta: { title: 'Dashboard', icon: 'dashboard', affix: true }
  //     }
  //   ]
  // }
]

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
export const asyncRoutes = [
  /* 用户 */
  {
    path: '/',
    component: TabLayout,
    meta: { title: '用户' },
    redirect: '/user/manage/userList',
    children: [
      {
        path: '/user/manage',
        component: Layout,
        meta: { title: '用户管理' },
        children: [
          {
            path: 'userList',
            name: 'UserList',
            component: () => import('@/views/user/user-manage/user-list'),
            meta: { title: '用户列表' }
          },
          {
            path: 'userRemoved',
            name: 'UserRemoved',
            component: () => import('@/views/user/user-manage/user-removed'),
            meta: { title: '已注销用户' }
          }
        ]
      }
    ]
  },
  /* 游戏 */
  {
    path: '/game',
    component: TabLayout,
    meta: { title: '游戏' },
    redirect: '/game/manage/gameList',
    children: [
      {
        path: '/game/manage',
        component: Layout,
        meta: { title: '游戏管理' },
        children: [
          {
            path: 'gameList',
            name: 'GameList',
            component: () => import('@/views/game/game-manage/game-list'),
            meta: { title: '游戏列表' }
          },
          {
            path: 'gameAccount',
            name: 'GameAccount',
            component: () => import('@/views/game/game-manage/game-account'),
            meta: { title: '游戏账号' }
          },
          {
            path: 'GameRoles',
            name: 'GameRoles',
            component: () => import('@/views/game/game-manage/game-roles'),
            meta: { title: '游戏角色' }
          },
          {
            path: 'GameClassify',
            name: 'GameClassify',
            component: () => import('@/views/game/game-manage/game-classify'),
            meta: { title: '游戏分类' }
          }
        ]
      },
      {
        path: '/game/channelManage',
        component: Layout,
        meta: { title: '渠道商管理' },
        children: [
          {
            path: 'ChannelList',
            name: 'ChannelList',
            component: () => import('@/views/game/channel-manage/channel-list'),
            meta: { title: '渠道商列表' }
          },
          {
            path: 'ChannelBonus',
            name: 'ChannelBonus',
            component: () => import('@/views/game/channel-manage/channel-bonus'),
            meta: { title: '渠道商分佣' }
          }
        ]
      },
      {
        path: '/game/giftBag',
        component: Layout,
        meta: { title: '礼包管理' },
        children: [
          {
            path: 'GiftBagList',
            name: 'GiftBagList',
            component: () => import('@/views/game/gift-bag/gift-bag-list'),
            meta: { title: '礼包列表' }
          },
          {
            path: 'GiftBagRecord',
            name: 'GiftBagRecord',
            component: () => import('@/views/game/gift-bag/gift-bag-record'),
            meta: { title: '礼包记录' }
          }
        ]
      },
      {
        path: '/game/coupons',
        component: Layout,
        meta: { title: '代金券管理' },
        children: [
          {
            path: 'CouponsList',
            name: 'CouponsList',
            component: () => import('@/views/game/coupons-manage/coupons-list'),
            meta: { title: '代金券列表' }
          },
          {
            path: 'CouponsRecord',
            name: 'CouponsRecord',
            component: () => import('@/views/game/coupons-manage/coupons-record'),
            meta: { title: '代金劵记录' }
          }
        ]
      },
      {
        path: '/game/rank',
        component: Layout,
        meta: { title: '游戏排行榜' },
        children: [
          {
            path: 'RankingList',
            name: 'RankingList',
            component: () => import('@/views/game/game-rank/ranking-list'),
            meta: { title: '游戏排名' }
          }
        ]
      }
    ]
  },
  /* 营销 */
  {
    path: '/marketing',
    component: TabLayout,
    meta: { title: '营销' },
    redirect: '/marketing/manage/missionList',
    children: [
      {
        path: '/marketing/manage',
        component: Layout,
        meta: { title: '营销管理' },
        children: [
          {
            path: 'missionList',
            name: 'MissionList',
            component: () => import('@/views/marketing/marketing-manage/mission-list'),
            meta: { title: '任务列表' }
          },
          {
            path: 'missionRecord',
            name: 'MissionRecord',
            component: () => import('@/views/marketing/marketing-manage/mission-record'),
            meta: { title: '任务记录' }
          }
        ]
      },
      {
        path: '/marketing/checkedIn',
        component: Layout,
        meta: { title: '签到管理' },
        children: [
          {
            path: 'checkedInRecord',
            name: 'CheckedInRecord',
            component: () => import('@/views/marketing/checked-in/checked-in-record'),
            meta: { title: '签到记录' }
          },
          {
            path: 'checkedInSetting',
            name: 'CheckedInSetting',
            component: () => import('@/views/marketing/checked-in/checked-in-setting'),
            meta: { title: '签到设置' }
          }
        ]
      }
    ]
  },
  /* 商城 */
  {
    path: '/mall',
    component: TabLayout,
    redirect: '/mall/manage/GoodsList',
    meta: { title: '商城' },
    children: [
      {
        path: '/mall/manage',
        component: Layout,
        meta: { title: '商城管理' },
        children: [
          {
            path: 'GoodsList',
            name: 'GoodsList',
            component: () => import('@/views/mall/mall-manage/goods-list'),
            meta: { title: '商品列表' }
          },
          {
            path: 'GoodsOrder',
            name: 'GoodsOrder',
            component: () => import('@/views/mall/mall-manage/goods-order'),
            meta: { title: '商品订单' }
          }
        ]
      },
      {
        path: '/mall/material',
        component: Layout,
        meta: { title: '道具管理' },
        children: [
          {
            path: 'MaterialList',
            name: 'MaterialList',
            component: () => import('@/views/mall/material-manage/material-list'),
            meta: { title: '道具列表' }
          },
          {
            path: 'MaterialUseRecord',
            name: 'MaterialUseRecord',
            component: () => import('@/views/mall/material-manage/material-use-record'),
            meta: { title: '道具使用记录' }
          }
        ]
      }
    ]
  },
  /* 交易 */
  {
    path: '/trade',
    component: TabLayout,
    redirect: '/trade/manage/TradeList',
    meta: { title: '交易' },
    children: [
      {
        path: '/trade/manage',
        component: Layout,
        meta: { title: '交易管理' },
        children: [
          {
            path: 'TradeList',
            name: 'TradeList',
            component: () => import('@/views/trade/trade-manage/trade-list'),
            meta: { title: '交易列表' }
          },
          {
            path: 'TradeAccount',
            name: 'TradeAccount',
            component: () => import('@/views/trade/trade-manage/trade-account'),
            meta: { title: '交易账号' }
          },
          {
            path: 'TradeRecord',
            name: 'TradeRecord',
            component: () => import('@/views/trade/trade-manage/trade-record'),
            meta: { title: '交易记录' }
          }
        ]
      }
    ]
  },
  /* 消息 */
  {
    path: '/message',
    component: TabLayout,
    redirect: '/message/manage/MessageList',
    meta: { title: '消息' },
    children: [
      {
        path: '/message/manage',
        component: Layout,
        meta: { title: '消息管理' },
        children: [
          {
            path: 'MessageList',
            name: 'MessageList',
            component: () => import('@/views/message/message-manage/message-list'),
            meta: { title: '消息列表' }
          },
          {
            path: 'MessageNotify',
            name: 'MessageNotify',
            component: () => import('@/views/message/message-manage/message-notify'),
            meta: { title: '消息通知' }
          }
        ]
      }
    ]
  },
  /* 财务 */

  /* app管理 */
  {
    path: '/app',
    component: TabLayout,
    redirect: '/app/manage/HomeBanner',
    meta: { title: 'app管理' },
    children: [
      {
        path: '/app/manage',
        component: Layout,
        meta: { title: 'App管理' },
        children: [
          {
            path: 'HomeBanner',
            name: 'HomeBanner',
            component: () => import('@/views/app/app-manage/home-banner'),
            meta: { title: '首页banner图' }
          },
          {
            path: 'RecommendWindows',
            name: 'RecommendWindows',
            component: () => import('@/views/app/app-manage/recommend-windows'),
            meta: { title: '推荐橱窗' }
          },
          {
            path: 'GameZone',
            name: 'GameZone',
            component: () => import('@/views/app/app-manage/game-zone'),
            meta: { title: '游戏专区' }
          },
          {
            path: 'ClassifyPage',
            name: 'ClassifyPage',
            component: () => import('@/views/app/app-manage/classify-page'),
            meta: { title: '分类页管理' }
          },
          {
            path: 'RankingManage',
            name: 'RankingManage',
            component: () => import('@/views/app/app-manage/ranking-manage'),
            meta: { title: '排行榜管理' }
          }
        ]
      }
    ]
  },
  /* 系统 */
  {
    path: '/system',
    component: TabLayout,
    redirect: '/system/manage/AdminList',
    meta: { title: '系统' },
    children: [
      {
        path: '/system/manage',
        component: Layout,
        meta: { title: '系统管理' },
        children: [
          {
            path: 'AdminList',
            name: 'AdminList',
            component: () => import('@/views/system/system-manage/admin-list'),
            meta: { title: '管理员列表' }
          }
        ]
      },
      {
        path: '/system/function',
        component: Layout,
        meta: { title: '功能管理' },
        children: [
          {
            path: 'RolesList',
            name: 'RolesList',
            component: () => import('@/views/system/function-manage/roles-list'),
            meta: { title: '角色列表' }
          }
        ]
      }
    ]
  },
  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
